{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "文档：\n",
    "\n",
    "- 插件下载地址：https://gstreamer.freedesktop.org/src/\n",
    "- https://github.com/pygobject/pgi-docs-devhelp/tree/9a1af5f10acf32c6f4b32f96a6bd3e6b668e63f7/python-GstRtspServer-1.0/classes\n",
    "\n",
    "- [PGI - Pure Python GObject Introspection Bindings](https://github.com/pygobject/pgi)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Linux下 python - Gobject 环境配置\n",
    "\n",
    "官方安装文档：[链接](https://gstreamer.freedesktop.org/documentation/installing/index.html?gi-language=python)\n",
    "\n",
    "## Gstreamer 安装\n",
    "\n",
    "```sh\n",
    "apt-get update\n",
    "apt-get install -y libgstreamer1.0-0 \\\n",
    "            gstreamer1.0-plugins-base \\\n",
    "            gstreamer1.0-plugins-good \\\n",
    "            gstreamer1.0-plugins-bad \\\n",
    "            gstreamer1.0-plugins-ugly \\\n",
    "            gstreamer1.0-libav \\\n",
    "            gstreamer1.0-doc \\\n",
    "            gstreamer1.0-tools \\\n",
    "            libgstreamer1.0-dev \\\n",
    "            libgstreamer-plugins-base1.0-dev \\\n",
    "            libgstreamer-plugins-good1.0-dev \\\n",
    "            libgstreamer-plugins-bad1.0-dev \\\n",
    "            gir1.2-gst-rtsp-server-1.0  \\\n",
    "\n",
    "```\n",
    "\n",
    "## Gobject - python 绑定\n",
    "\n",
    "**Windows可以在子系统中使用，Linux中自带的python环境中一般都有 gi 模块**\n",
    "\n",
    "---\n",
    "\n",
    "初次使用Gstreamer时经常会遇到gi包不存在的问题。\n",
    "```py\n",
    "import gi\n",
    "```\n",
    "\n",
    "报错如下：\n",
    "```\n",
    "ImportError: No module named gi\n",
    "```\n",
    "\n",
    "而且使用python的pip安装往往会出现找不到gi版本\n",
    "\n",
    "## 安装方法\n",
    "\n",
    "### 系统自带python环境中\n",
    "\n",
    "首先在终端输入命令来查看ubuntu的python库中有无gi包：\n",
    "\n",
    "```\n",
    "python3\n",
    "import gi\n",
    "```\n",
    "\n",
    "如果和程序中报相同的错误，那么就表明此python库中没有gi包。\n",
    "我们先使用命令在python库中添加这个包：\n",
    "\n",
    "```sh\n",
    "sudo apt-get install python3-gi\n",
    "```\n",
    "\n",
    "显示安装成功后可以再在终端中 **使用系统自带的python3版本**中使用import gi命令来测试是否可以导入。\n",
    "\n",
    "\n",
    "### Anaconda或者其他环境中\n",
    "\n",
    "**先执行**：\n",
    "```sh\n",
    "sudo apt-get install python3-gi\n",
    "```\n",
    "或者，如果需要Python2支持，请执行以下操作：\n",
    "\n",
    "```sh\n",
    "sudo apt-get install python-gi\n",
    "```\n",
    "\n",
    "1. 方法一：推荐\n",
    "\n",
    "先查看以上命令是否安装`python-gi`成功\n",
    "```sh\n",
    "cd /usr/lib/python3/dist-packages/\n",
    "ls gi\n",
    "```\n",
    "如果有gi包, 将其复制到你需要的python环境下，比如 anaconda：\n",
    "\n",
    "```sh\n",
    "cp gi /home/xxx//anaconda3/lib/python3.7/site-packages -r\n",
    "```\n",
    "\n",
    "然后将 gi 目录下的 _gi.cpython-36m-x86_64-linux-gnu.so, 重命名\n",
    "(目的，将cython版本隐藏，或者换成你需要的python版本）：\n",
    "\n",
    "```sh\n",
    "mv _gi.cpython-36m-x86_64-linux-gnu.so  _gi.so\n",
    "mv _gi_cairo.cpython-36m-x86_64-linux-gnu.so  _gi_cairo.so\n",
    "```\n",
    "\n",
    "目录下还可能有其他 .so 文件，全都把文件名中间部分的去掉即可，然后就可以愉快的玩耍了。\n",
    "\n",
    "2. 方法二：亲测有用\n",
    "\n",
    "[【pgi】](https://github.com/pygobject/pgi)（Gobject\n",
    "纯python绑定，许多东西还不完善，查看example支持的方法） \n",
    "\n",
    "> pip install pgi\n",
    "\n",
    "```py\n",
    "import pgi\n",
    "pgi.install_as_gi()\n",
    "from gi.repository import Gtk, GObject\n",
    "```\n",
    "3. 方法三：\n",
    "从github 或者 码云上搜索gi文件夹里的一串代码，比如tepes.py 里的：\n",
    "\n",
    "```py\n",
    "def find_vfunc_conflict_in_bases(vfunc, bases):\n",
    "```\n",
    "\n",
    "然后找到目录结构类似于\n",
    "\n",
    "```\n",
    "lib/python3.6/site-packages/gi/types.py\n",
    "或者\n",
    "lib/python3.7/site-packages/gi/types.py\n",
    "```\n",
    "\n",
    "将项目克隆下来，然后将gi文件夹放到自己的 lib/python3.xx/site-packages 中\n",
    "\n",
    "4. 方法四：自己编译\n",
    "\n",
    "  项目地址： [码云](https://gitee.com/industiosoft/sbc6y11_buildroot_build/tree/master/python-gobject-2.28.6)\n",
    "\n",
    "\n",
    "4. 方法四：对于 virtualenv 用户- [VEXT](https://stackoverflow.com/a/43808204/5209935) 方法\n",
    "  \n",
    "  安装以下包\n",
    "  >pip install vext  \n",
    "  >pip install vext.gi\n",
    "\n",
    "5. 纯 python 开发人员方法：(亲测安装失败）\n",
    "\n",
    "  参考：[链接](https://pygobject.readthedocs.io/en/latest/getting_started.html#ubuntu-getting-started)\n",
    "\n",
    "  安装一组开发人员内容：\n",
    "\n",
    "  >sudo apt-get install pkg-config libcairo2-dev gcc python3-dev libgirepository1.0-dev\n",
    "\n",
    "  安装 python 软件包：\n",
    "\n",
    "  >pip install gobject PyGObject\n",
    "\n",
    "**安装位置**：\n",
    "\n",
    "上述方法会安装在系统自带的python-lib目录下：\n",
    "\n",
    "```\n",
    "cd /usr/lib/python3/dist-packages 查看有无 gi 文件夹\n",
    "```\n",
    "\n",
    "**注意**：不要使用以下命令安装，否则无法正常使用\n",
    "\n",
    "```sh\n",
    "conda install -c conda-forge pygobject\n",
    "```\n",
    "\n",
    "参考：\n",
    "\n",
    "1. [How to install gi module for anaconda python3?](https://stackoverflow.com/questions/37526026/how-to-install-gi-module-for-anaconda-python3/48400789#48400789?newreg=88dc0dc9e14b46e88f6ffbfa03b8ac7c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 编译安装支持gstreamer版本的opencv\n",
    "\n",
    "## 安装依赖项\n",
    "\n",
    "```sh\n",
    "apt-get install -y \\\n",
    "        build-essential \\\n",
    "        cmake \\\n",
    "        git \\\n",
    "        pkg-config \n",
    "        \n",
    "    \n",
    "# OpenCV官网中安装的是旧版本的libgtk2.0-dev，这里我们安装新版本的libgtk-3-dev\n",
    "apt-get install -y libgtk-3-dev \\\n",
    "        libavcodec-dev \\\n",
    "        libavformat-dev \\\n",
    "        libswscale-dev \\\n",
    "        libv4l-dev \\\n",
    "        libavutil-dev \\\n",
    "        libavresample-dev \\\n",
    "        libpostproc-dev \\\n",
    "        libxvidcore-dev \\\n",
    "        libatlas-base-dev \\\n",
    "        libx264-dev \\\n",
    "        gfortran \\\n",
    "        libhdf5-dev \\\n",
    "        liblapacke-dev \\\n",
    "        checkinstall \\\n",
    "        libglib2.0-dev \\\n",
    "        libopenexr-dev\n",
    "        \n",
    "apt-get install ffmpeg\n",
    "\n",
    "# OpenCV官网中安装2.7版本的python-dev和python-numpy，这里我们安装python 3版本的python3-dev和python3-numpy\n",
    "apt-get install python3-dev python3-numpy python3-py python3-pytest libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev libglew-dev zlib1g-dev libtiff5-dev\n",
    "\n",
    "apt install -y gfortran libblas-dev liblapack-dev libeigen3-dev \n",
    "apt install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev\n",
    "\n",
    "# 安装 libjasper\n",
    "sudo add-apt-repository \"deb http://security.ubuntu.com/ubuntu xenial-security main\"\n",
    "sudo apt update\n",
    "sudo apt install libjasper1 libjasper-dev\n",
    "```\n",
    "\n",
    "## 下载opencv - 4.1.1 源码\n",
    "\n",
    "- [opencv- 4.1.1下载地址](https://github.com/opencv/opencv/archive/4.1.1.tar.gz)\n",
    "- [opencv-contrib-4.1.1下载地址](https://github.com/opencv/opencv_contrib/archive/4.1.1.tar.gz)\n",
    "\n",
    "## 解压\n",
    "\n",
    "```sh\n",
    "tar -zxvf opencv-4.1.1.tar.gz\n",
    "tar -zxvf opencv_contrib-4.1.1.tar.gz\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用cmake-gui安装\n",
    "\n",
    "###  安装cmake-qt-gui\n",
    "```sh\n",
    "sudo apt-get install cmake\n",
    "sudo apt-get install cmake-qt-gui\n",
    "```\n",
    "\n",
    "###  在opencv-4.1.1中建立build目录\n",
    "\n",
    "  打开cmake-gui，设定source code目录为/home/your-name/opencv-4.1.1/，设定binaries目录为刚才新建的build目录，也就是/home/your-name/opencv-4.1.1/build\n",
    "  \n",
    "![pic](../../assets/opencv-cmake-gui.png)\n",
    "\n",
    "点击Configure\n",
    "![pic](../../assets/cmake-opencv1.png)\n",
    "\n",
    "### Configure 结束后，如果 cmake 的主界面仍有红色区域，则再次点击 Configure 进行配置，直到红色区域完全消失。\n",
    "\n",
    "### 红色区域消失后，进行下一步配置\n",
    "\n",
    "    - 在 CMAKE_BUILD_TYPE 值处选择 Release\n",
    "    - CMAKE_INSTALL_PREFIX 选择安装的路径，默认（/usr/local）\n",
    "    - **在 OPENCV_EXTRA_MODULES_PATH 处，为其设置 opencv_contrib 的路径，精确到 /modules 目录**\n",
    "    - 勾选 OPENCV_PYTHON3_VERSION\n",
    "    - 把python2的执行器去掉（目的是使用python3的执行器）\n",
    "    - 勾选 OPENCV_GENERATE_PKGCONFIG\n",
    "    - 输入CL，分别将WITH_OPENCL, WITH_OPENCLAMDBLAS, WITH_OPENCLAMDFFT, OPENCL_FOUND的勾去掉，不采用OpenCL的GPU加速；\n",
    "    - 输入matlab，相关的去掉；\n",
    "    - 输入WITH_IPP，去掉；\n",
    "    - 输入TIFF，还有其他的jpeg,png之类都选上；\n",
    "    - 添加选项ENABLE_CXX11，支持C++11;\n",
    "    - 搜索cuda，根据硬件要求自行配置。\n",
    "    \n",
    "### 生成CMakefile文件\n",
    "\n",
    "点击Configure，直至没有红色区域出现，然后点击Generate, 查看cmake-gui下方日志，有没有`Error`信息，并解决，`Warning`信息可略过不影响安装。\n",
    "\n",
    "![pic](../../assets/cmake-opencv2.png)\n",
    "\n",
    "ps：配置输出信息中，python2或者python3的内容要如图所示才是正确的。由于py2 已经停止维护，而py3在annaconda的环境中使用比较方便，贫道遂放弃使用系统自带的py版本，如下图。\n",
    "\n",
    "![pic](../../assets/cmake-opencv3.png)\n",
    "下图红框中的路径全都替换成`/usr/lib/x86_64-linux-gnu/xxx.so` 的路径也行，但不要既有conda下的又有系统路径的。\n",
    "![pic](../../assets/cmake-opencv5.png)\n",
    "![pic](../../assets/cmake-opencv6.png)\n",
    "\n",
    "如果此处有问题，请参考[链接](https://blog.csdn.net/Lin3119333/article/details/100022079)\n",
    "\n",
    "### 消出警告信息（可省略，但可能影响使用）\n",
    "\n",
    "常见警告是由于文件下载失败导致的，可以开个VPN，修改.bashrc文件\n",
    "\n",
    "```sh\n",
    "export http_proxy=http://127.0.0.1:7890\n",
    "export https_proxy=http://127.0.0.1:7890\n",
    "```\n",
    "\n",
    "在cmake-gui的下方输出日志中查看日志是否有以下信息：\n",
    "\n",
    "**警告一：**\n",
    "\n",
    "```\n",
    "IPPICV: Download: ippicv_2019_lnx_intel64_general_20180723.tgz\n",
    "IPPICV: Download failed: 7;\"Couldn't connect to server\"\n",
    "```\n",
    "比如我这里显示ippicv_2019_lnx_intel64_general_20180723.tgz下载失败，去百度或者csdn下载对应文件.  \n",
    "保存路径随意，我是放在默认的下载路径 ~/DownLoad/  \n",
    "然后修改opencv里相关配置文件\n",
    "\n",
    "```sh\n",
    "# 打开终端，输入\n",
    " \n",
    "gedit ~/DownLoad/opencv_source/opencv/3rdparty/ippicv/ippicv.cmake #就是这个文件的路径\n",
    " \n",
    "# 将47行的\n",
    "\"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}ippicv/\"\n",
    "\n",
    "# 改为步骤1中手动下载的文件的本地路径(也就是将网络下载的模式改为本地文件下载的模式):\n",
    " \n",
    "\"file:~/Downloads/\" #（仅供参考，根据自己的路径填写）\n",
    "```\n",
    "\n",
    "**警告二**： 模型缺失\n",
    "\n",
    "  在线下载，或者按照文件名下载离线文件放到.cache 文件夹里对应的目录下，并按照其目录下原有的大小为0kb的文件名重命名。\n",
    "\n",
    "  参考：<https://github.com/opencv/opencv_contrib/issues/1301>\n",
    "  参考：<https://www.jianshu.com/p/f1d836295a60>\n",
    "\n",
    "```sh\n",
    "cd opencv-1.1.1./cache\n",
    "wget  https://raw.githubusercontent.com/opencv/opencv_3rdparty/8afa57abc8229d611c4937165d20e2a2d9fc5a12/face_landmark_model.dat -o face_landmark_model.dat\n",
    "wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/ippicv/master_20180723/ippicv/ippicv_2019_lnx_intel64_general_20180723.tgz -o ippicv_2019_lnx_intel64_general_20180723.tgz\n",
    "\n",
    "\n",
    "cd ./xfeatures2d/\n",
    "cd boostdesc\n",
    "\n",
    "wget -o 0ae0675534aa318d9668f2a179c2a052-boostdesc_lbgm.i https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_lbgm.i \n",
    "wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_256.i -o e6dcfa9f647779eb1ce446a8d759b6ea-boostdesc_binboost_256.i\n",
    "wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_128.i -o 98ea99d399965c03d555cef3ea502a0b-boostdesc_binboost_128.i\n",
    "wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_064.i -o 202e1b3e9fec871b04da31f7f016679f-boostdesc_binboost_064.i\n",
    "wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_hd.i -o 324426a24fa56ad9c5b8e3e0b3e5303e-boostdesc_bgm_hd.i\n",
    "wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_bi.i -o 232c966b13651bd0e46a1497b0852191-boostdesc_bgm_bi.i\n",
    "wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm.i -o 0ea90e7a8f3f7876d450e4149c97c74f-boostdesc_bgm.i\n",
    "cd ../vgg\n",
    "wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_120.i -o 151805e03568c9f490a5e3a872777b75-vgg_generated_120.i\n",
    "wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_64.i -o 7126a5d9a8884ebca5aea5d63d677225-vgg_generated_64.i\n",
    "wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_48.i -o e8d0dcd54d1bcfdc29203d011a797179-vgg_generated_48.i\n",
    "wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_80.i -o 7cd47228edec52b6d82f46511af325c5-vgg_generated_80.i\n",
    " \n",
    "```\n",
    "\n",
    "\n",
    "6. 编译安装OpenCV\n",
    "生成Makefile之后，进入/home/your-name/opencv-4.1.1/build，编译并安装。然后是漫长的等待过程。\n",
    "\n",
    "```sh\n",
    "make -j4\n",
    "sudo make install\n",
    "sudo ldconfig\n",
    "```\n",
    "\n",
    "7. 验证\n",
    "    - 验证pkg-config功能是否正确\n",
    "    ```sh\n",
    "    pkg-config --modversion opencv\n",
    "    或者\n",
    "    pkg-config –libs opencv\n",
    "    ```\n",
    "    若能输出版本信息，则说明opencv已经安装成功\n",
    "    \n",
    "    - 验证opencv_python3是否正确安装。\n",
    "    ```\n",
    "    $python3\n",
    "    >>>import cv2\n",
    "    >>> print(cv2.__version__)\n",
    "    ```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用cmake命令安装\n",
    "\n",
    "```sh\n",
    "cmake -D CMAKE_BUILD_TYPE=RELEASE \\\n",
    "-D CMAKE_INSTALL_PREFIX=/usr/local \\\n",
    "-D WITH_GSTREAMER=ON \\\n",
    "-D INSTALL_C_EXAMPLES=OFF \\\n",
    "-D INSTALL_PYTHON_EXAMPLES=ON \\\n",
    "-D BUILD_EXAMPLES=ON \\\n",
    "-D PYTHON_EXECUTABLE=/usr/bin/python3 \\\n",
    "-D PYTHON_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.6/dist-packages/numpy/core/include/ \\\n",
    "-D WITH_GTHREAD=ON \\\n",
    "-D WITH_TBB=ON \\\n",
    "-D WITH_OPENGL=ON \\\n",
    "-D OPENCV_GENERATE_PKGCONFIG=YES  ..\n",
    "-D BUILD_opencv_python3=ON\n",
    "sudo ldconfig\n",
    "---\n",
    "-D WITH_QT=ON \\\n",
    "-D WITH_OPENGL=ON \\ \n",
    "-D FORCE_VTK=ON \\\n",
    "-D WITH_TBB=ON \\\n",
    "-D WITH_GDAL=ON \\\n",
    "-D WITH_XINE=ON \\\n",
    "-D BUILD_EXAMPLES=ON \\\n",
    "-D BUILD_TIFF=ON \n",
    "-D BUILD_opencv_java=OFF \\ \n",
    "-D WITH_CUDA=OFF \\\n",
    "-D ENABLE_AVX=ON \\\n",
    "-D WITH_OPENGL=ON \\\n",
    "-D WITH_OPENCL=ON \\\n",
    "-D WITH_IPP=ON \\\n",
    "-D WITH_TBB=ON \\\n",
    "-D WITH_EIGEN=ON \\\n",
    "-D WITH_V4L=ON \\\n",
    "-D WITH_VTK=OFF \\\n",
    "-D BUILD_TESTS=OFF \\\n",
    "-D BUILD_PERF_TESTS=OFF \\\n",
    "-D BUILD_opencv_python2=OFF \n",
    "-D PYTHON3_INCLUDE_DIR=$(python3 -c \"from distutils.sysconfig import get_python_inc; print(get_python_inc())\") \n",
    "-D PYTHON3_PACKAGES_PATH=$(python3 -c \"from distutils.sysconfig import get_python_lib; print(get_python_lib())\")  ..  \n",
    "\n",
    "开启CUDA支持\n",
    "\n",
    "-D PYTHON_DEFAULT_EXECUTABLE=$(python3 -c \"import sys; print(sys.executable)\")   \\\n",
    "-D PYTHON3_EXECUTABLE=$(python3 -c \"import sys; print(sys.executable)\")   \\\n",
    "-D PYTHON3_NUMPY_INCLUDE_DIRS=$(python3 -c \"import numpy; print (numpy.get_include())\") \\\n",
    "-D PYTHON3_PACKAGES_PATH=$(python3 -c \"from distutils.sysconfig import get_python_lib; print(get_python_lib())\") \\\n",
    "-D BUILD_CUDA_STUBS=ON \\\n",
    "-D WITH_CUDA=ON \\\n",
    "-D ENABLE_FAST_MATH=1 \\\n",
    "-D CUDA_FAST_MATH=1 \\\n",
    "-D WITH_CUBLAS=1 \\\n",
    "-D BUILD_DOCS=ON \\\n",
    "-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.1.0/modules \\\n",
    "    ..\n",
    "```\n",
    "\n",
    "**注意**：make 过程可能失败，检查错误原因 ，再`make clean `可以清除make失败的内容，然后重新`make ` ，  `make install`\n",
    "\n",
    "---\n",
    "\n",
    "## 其他配置\n",
    "\n",
    "如果使用的是系统python3版本安装的，需要链接到anaconda中使用：  \n",
    "将cv2链接到python3.7的库文件路径中\n",
    "\n",
    "```sh\n",
    "sudo ln -s /usr/local/lib/python3.7/site-packages/cv2 /home/lin/anaconda3/lib/python3.7/cv2\n",
    "```\n",
    "\n",
    "或者\n",
    "```sh\n",
    "sudo cp ~/opencv/opencv-4.1.1/build/lib/cv2.so ~/anaconda3/lib/python3.7/site-packages/\n",
    "```\n",
    "\n",
    "## C++环境配置\n",
    "\n",
    "参考：<https://blog.csdn.net/Lin3119333/article/details/100022079>\n",
    "\n",
    "## 可能出现的错误\n",
    "\n",
    "**错误一：**\n",
    "\n",
    "`fatal error: Eigen/Core: No such file or directory`\n",
    "\n",
    "**解决：**\n",
    "\n",
    "这是因为 eigen 库默认安装在了 /usr/include/eigen3/Eigen 路径下，需使用下面命令映射到 /usr/include 路径下\n",
    "\n",
    "sudo ln -s /usr/include/eigen3/Eigen /usr/include/Eigen\n",
    "\n",
    "另外若还是失败，`make clean`后重新打开cmake-gui，在Search里搜索Eigen，修改`EIGEN_INCLUDE_PATH`值为/usr/include/eigen3\n",
    "\n",
    "\n",
    "`EIGEN_INCLUDE_PATH=/opt/local/include/eigen3`\n",
    "或者选择禁用：\"-D WITH_EIGEN=OFF\"\n",
    "\n",
    "**错误二**\n",
    "\n",
    "... fatal error: dc1394/dc1394.h: No such file or directory\n",
    "\n",
    "**解决方案：**\n",
    "\n",
    "在cmake命令行上加`-D WITH_1394=OFF`\n",
    "\n",
    "**错误三**\n",
    "\n",
    "如果`sudo ldconfig`时出现下面错误：\n",
    "\n",
    "/sbin/ldconfig.real: /usr/local/cuda-10.0/targets/x86_64-linux/lib/libcudnn.so.7 is not a symbolic link\n",
    "\n",
    "**解决方案**：\n",
    "```\n",
    "$ cd /usr/local/cuda/lib64\n",
    "$ ls -lha libcudnn*\n",
    "-rwxr-xr-x 3 root root 333M Jan 27 17:06 libcudnn.so\n",
    "-rwxr-xr-x 3 root root 333M Jan 27 17:06 libcudnn.so.7\n",
    "-rwxr-xr-x 3 root root 333M Jan 27 17:06 libcudnn.so.7.4.2\n",
    "-rw-r--r-- 1 root root 331M Jan 27 17:06 libcudnn_static.a\n",
    "\n",
    "$ sudo rm libcudnn.so\n",
    "$ sudo rm libcudnn.so.7\n",
    "$ sudo ln libcudnn.so.7.4.2 libcudnn.so.7\n",
    "$ sudo ln libcudnn.so.7 libcudnn.so\n",
    "$ sudo ldconfig\n",
    "```\n",
    "\n",
    "---\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 清理安装\n",
    "build目录有11GB，在sudo make install之后，这个目录没有保留的必要了，删掉即可。\n",
    "\n",
    "## 卸载opencv\n",
    "\n",
    "如果需要卸载，就是简单粗暴删文件。\n",
    "\n",
    "```sh\n",
    "$ cd /usr\n",
    "$ sudo find . -name \"*opencv*\" | xargs sudo rm -rf \n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 参考\n",
    "\n",
    "- [Ubuntu下源码安装opencv3.4.5至anaconda（支持python3 、Python2、C++）](https://blog.csdn.net/Lin3119333/article/details/100022079)\n",
    "- [opencv配置选项详解](https://blog.csdn.net/qq_38880380/article/details/80987661)\n",
    "- [TX2+GStreamer+OpenCV读取显示网络摄像头](https://blog.csdn.net/zong596568821xp/article/details/80306987)\n",
    "- https://www.cnblogs.com/laizhenghong2012/p/11329916.html\n",
    "- https://www.cnblogs.com/journeyonmyway/p/10320837.html\n",
    "- [opencv 解决ippicv下载问题，离线:ippicv_2019_lnx_intel64_general_20180723.tgz](https://blog.csdn.net/orDream/article/details/84311697)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**可能需要的依赖包：apt install xxx**\n",
    "\n",
    "```\n",
    "python3-gi\n",
    "\n",
    "gir1.2-gst-rtsp-server-1.0 \n",
    "\n",
    "gir1.2-gtk-3.0,\n",
    "gir1.2-gst-plugins-base-1.0\n",
    "\n",
    "gir1.2-gst-plugins-base-0.1  \n",
    "gir1.2-gstreamer-0.10  \n",
    "python3-cairo  \n",
    "python3-gi-cairo  \n",
    "gnome-icon-theme-symbolic \n",
    "\n",
    "gstreamer1.0  \n",
    "\n",
    "gstreamer0.10-ffmpeg  \n",
    "gstreamer1.0-tools \n",
    "gstreamer1.0-alsa \n",
    "gstreamer1.0-plugins-base\n",
    "gstreamer1.0-plugins-good\n",
    "gstreamer1.0-plugins-bad\n",
    "gstreamer1.0-plugins-ugly\n",
    "gstreamer1.0-libav\n",
    "gstreamer1.0-vaapi\n",
    "\n",
    "libgstreamer1.0-dev\n",
    "libgstreamer-plugins-base1.0-dev\n",
    "libgstreamer-plugins-good1.0-dev \n",
    "libgstreamer-plugins-bad1.0-dev\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 硬件加速\n",
    "\n",
    "gstreamer1.0-vaapi 安装问题：\n",
    "\n",
    "查看插件安装情况：\n",
    "\n",
    ">gst-inspect-1.0 vaapi\n",
    "\n",
    "**问题：**\n",
    "最后发现 0 feature： ？？？？没有安装上插件\n",
    "\n",
    "原因：硬件不支持\n",
    "\n",
    "详细信息：\n",
    "\n",
    "- [Problem installing GStreamer-vaapi](http://gstreamer-devel.966125.n4.nabble.com/Problem-installing-GStreamer-vaapi-td4683225.html)\n",
    "\n",
    "- [Linux与视频文件的硬解播放](http://mtoou.info/327.html)\n",
    "\n",
    "**解决方法**\n",
    "\n",
    "1. 首先需要GPU（硬解）环境，否则只能用CPU 软解\n",
    "2. 安装 `libav`驱动环境，  \n",
    " I think ubuntu packages these drivers in packages like\n",
    "i965-va-driver and mesa-va-drivers. \n",
    "3. 输入 `vainfo` 查看系统支持的音视频编解码能力，比如：\n",
    "\n",
    "```\n",
    "libva info: VA-API version 0.40.0\n",
    "libva info: va_getDriverName() returns 0\n",
    "libva info: Trying to open /lib64/dri/i965_drv_video.so\n",
    "libva info: Found init function __vaDriverInit_0_40\n",
    "libva info: va_openDriver() returns 0\n",
    "vainfo: VA-API version: 0.40 (libva )\n",
    "vainfo: Driver version: Intel i965 driver for Intel(R) Skylake -\n",
    "1.8.1.pre1 (1.7.3-360-gcf087db)\n",
    "vainfo: Supported profile and entrypoints\n",
    "       VAProfileMPEG2Simple            : VAEntrypointVLD\n",
    "       VAProfileMPEG2Simple            : VAEntrypointEncSlice\n",
    "       VAProfileMPEG2Main              : VAEntrypointVLD\n",
    "       VAProfileMPEG2Main              : VAEntrypointEncSlice\n",
    "       VAProfileH264ConstrainedBaseline: VAEntrypointVLD\n",
    "       VAProfileH264ConstrainedBaseline: VAEntrypointEncSlice\n",
    "       VAProfileH264ConstrainedBaseline: VAEntrypointEncSliceLP\n",
    "       VAProfileH264Main               : VAEntrypointVLD\n",
    "       VAProfileH264Main               : VAEntrypointEncSlice\n",
    "       VAProfileH264Main               : VAEntrypointEncSliceLP\n",
    "       VAProfileH264High               : VAEntrypointVLD\n",
    "       VAProfileH264High               : VAEntrypointEncSlice\n",
    "       VAProfileH264High               : VAEntrypointEncSliceLP\n",
    "       VAProfileH264MultiviewHigh      : VAEntrypointVLD\n",
    "       VAProfileH264MultiviewHigh      : VAEntrypointEncSlice\n",
    "       VAProfileH264StereoHigh         : VAEntrypointVLD\n",
    "       VAProfileH264StereoHigh         : VAEntrypointEncSlice\n",
    "       VAProfileVC1Simple              : VAEntrypointVLD\n",
    "       VAProfileVC1Main                : VAEntrypointVLD\n",
    "       VAProfileVC1Advanced            : VAEntrypointVLD\n",
    "       VAProfileNone                   : VAEntrypointVideoProc\n",
    "       VAProfileJPEGBaseline           : VAEntrypointVLD\n",
    "       VAProfileJPEGBaseline           : VAEntrypointEncPicture\n",
    "       VAProfileVP8Version0_3          : VAEntrypointVLD\n",
    "       VAProfileVP8Version0_3          : VAEntrypointEncSlice\n",
    "       VAProfileHEVCMain               : VAEntrypointVLD\n",
    "       VAProfileHEVCMain               : VAEntrypointEncSlice\n",
    "       VAProfileVP9Profile0            : VAEntrypointVLD\n",
    "```              \n",
    "\n",
    "4. 重新安装 gstreamer-vaapi（可以编译安装其他版本），安装过程中会根据上面输出的设备能力集注册相应的插件。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "测试插件是否安装：\n",
    "\n",
    "```sh\n",
    "gst-inspect-1.0 --version   # 确认安装该插件\n",
    "\n",
    "gst-inspect-1.0 shmsrc\n",
    "gst-inspect-1.0 xxxxx\n",
    "```\n",
    "\n",
    "### 参考\n",
    "\n",
    "\n",
    "- 安装包：https://gstreamer.freedesktop.org/src/\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "220.42px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
